package com.aismy.admin.controller;

import com.aismy.admin.dto.ScaleSubmitDTO;
import com.aismy.admin.dto.ScaleResultDTO;
import com.aismy.admin.entity.EvalScale;
import com.aismy.admin.service.EvalScaleService;

import com.aismy.admin.vo.QuestionVO;
import com.aismy.admin.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/scales")
@RequiredArgsConstructor
public class EvalScaleController {

    private final EvalScaleService scaleService;

    @GetMapping("/{id}")
    public Result<EvalScale> getScaleDetail(@PathVariable("id") Long id) {
        return Result.success(scaleService.getScaleDetail(id));
    }

    @GetMapping("one/{id}")
    public Result<EvalScale> getEvalScale(@PathVariable Long id){
        return Result.success(scaleService.getById(id));
    }

    @PutMapping("/{id}")
    public Result<Void> updateScale(@PathVariable("id") Long id, @RequestBody EvalScale scale) {
        scale.setId(id);
        scaleService.updateById(scale);
        return Result.success();
    }

    @GetMapping("")
    public Page<EvalScale> getScalePage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String query) {
        return scaleService.getScalePage(page, size, query);
    }

    @PostMapping
    public Result<Void> addScale(@RequestBody EvalScale scale) {
        scaleService.save(scale);
        return Result.success();
    }

    @PutMapping("/status/{id}")
    public Result<Void> updateStatus(@PathVariable("id") Long id, @RequestParam Integer status) {
        scaleService.updateStatus(id, status);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteScale(@PathVariable("id") Long id) {
        scaleService.removeById(id);
        return Result.success();
    }

    @PostMapping("/{id}/submit")
    public Result<Long> submitEvaluation(
            @PathVariable("id") Long id,
            @RequestBody ScaleSubmitDTO submitDTO) {
        return Result.success(scaleService.submitEvaluation(id, submitDTO));
    }

    @GetMapping("/{id}/result")
    public Result<ScaleResultDTO> getEvaluationResult(@PathVariable("id") Long id) {
        return Result.success(scaleService.getEvaluationResult(id));
    }

    /**
     * 获取量表的所有题目及选项
     */
    @GetMapping("/{id}/questions")
    public Result<List<QuestionVO>> getScaleQuestions(@PathVariable("id") Long id) {
        return Result.success(scaleService.getScaleQuestions(id));
    }
} 